// 文件路径: com.city.java.abnormal_records.service.impl.AbnormalProcessingServiceImpl.java
package com.city.java.abnormal_records.service.impl;

import com.city.java.abnormal_records.mapper.AbnormalProcessingMapper;
import com.city.java.abnormal_records.pojo.entity.AbnormalProcessing;
import com.city.java.base.response.JsonResult;
import com.city.java.base.response.StatusCode;
import com.city.java.abnormal_records.service.AbnormalProcessingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 异常记录处理服务实现类
 */
@Slf4j
@Service
public class AbnormalProcessingServiceImpl implements AbnormalProcessingService {

    @Autowired
    private AbnormalProcessingMapper abnormalProcessingMapper;

    @Override
    public JsonResult addAbnormalProcessing(AbnormalProcessing abnormalProcessing) {
        log.debug("准备添加异常记录处理: {}", abnormalProcessing);

        // 参数校验
        if (abnormalProcessing == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常记录处理信息不能为空");
        }

        if (abnormalProcessing.getAbnormalId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常记录ID不能为空");
        }

        // 设置创建时间
        if (abnormalProcessing.getCreatedAt() == null) {
            abnormalProcessing.setCreatedAt(LocalDateTime.now());
        }

        // 设置默认状态
        if (abnormalProcessing.getStatus() == null) {
            abnormalProcessing.setStatus(AbnormalProcessing.ProcessingStatus.pending);
        }

        try {
            abnormalProcessingMapper.insertAbnormalProcessing(abnormalProcessing);
            log.info("异常记录处理添加成功: 处理ID={}", abnormalProcessing.getProcessingId());
            return JsonResult.ok();
        } catch (Exception e) {
            log.error("添加异常记录处理失败: 异常记录ID={}", abnormalProcessing.getAbnormalId(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "添加异常记录处理失败");
        }
    }

    @Override
    public JsonResult getAbnormalProcessingById(Long processingId) {
        log.debug("根据ID查询异常记录处理: {}", processingId);

        // 参数校验
        if (processingId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "处理ID不能为空");
        }

        try {
            AbnormalProcessing abnormalProcessing = abnormalProcessingMapper.selectById(processingId);
            if (abnormalProcessing != null) {
                log.info("查询异常记录处理成功: ID={}", processingId);
                return JsonResult.ok(abnormalProcessing);
            } else {
                log.warn("异常记录处理不存在: ID={}", processingId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "异常记录处理不存在");
            }
        } catch (Exception e) {
            log.error("查询异常记录处理失败: ID={}", processingId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询异常记录处理失败");
        }
    }

    @Override
    public JsonResult getAbnormalProcessingByAbnormalId(Long abnormalId) {
        log.debug("根据异常记录ID查询异常记录处理: {}", abnormalId);

        // 参数校验
        if (abnormalId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常记录ID不能为空");
        }

        try {
            AbnormalProcessing abnormalProcessing = abnormalProcessingMapper.selectByAbnormalId(abnormalId);
            if (abnormalProcessing != null) {
                log.info("查询异常记录处理成功: 异常记录ID={}", abnormalId);
                return JsonResult.ok(abnormalProcessing);
            } else {
                log.warn("异常记录处理不存在: 异常记录ID={}", abnormalId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "异常记录处理不存在");
            }
        } catch (Exception e) {
            log.error("查询异常记录处理失败: 异常记录ID={}", abnormalId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询异常记录处理失败");
        }
    }

    @Override
    public JsonResult getAbnormalProcessingsByStatus(AbnormalProcessing.ProcessingStatus status) {
        log.debug("根据状态查询异常记录处理列表: {}", status);

        // 参数校验
        if (status == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "状态不能为空");
        }

        try {
            List<AbnormalProcessing> abnormalProcessings = abnormalProcessingMapper.selectByStatus(status);
            log.info("查询状态异常记录处理列表成功: 状态={}, 记录数量={}", status, abnormalProcessings.size());
            return JsonResult.ok(abnormalProcessings);
        } catch (Exception e) {
            log.error("查询状态异常记录处理列表失败: 状态={}", status, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询异常记录处理列表失败");
        }
    }

    @Override
    public JsonResult getAllAbnormalProcessings() {
        log.debug("查询所有异常记录处理列表");

        try {
            List<AbnormalProcessing> abnormalProcessings = abnormalProcessingMapper.selectAll();
            log.info("查询所有异常记录处理列表成功: 记录数量={}", abnormalProcessings.size());
            return JsonResult.ok(abnormalProcessings);
        } catch (Exception e) {
            log.error("查询所有异常记录处理列表失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询异常记录处理列表失败");
        }
    }

    @Override
    public JsonResult deleteAbnormalProcessingById(Long processingId) {
        log.debug("根据ID删除异常记录处理: {}", processingId);

        // 参数校验
        if (processingId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "处理ID不能为空");
        }

        try {
            int result = abnormalProcessingMapper.deleteById(processingId);
            if (result > 0) {
                log.info("删除异常记录处理成功: ID={}", processingId);
                return JsonResult.ok();
            } else {
                log.warn("删除异常记录处理失败，异常记录处理不存在: ID={}", processingId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "异常记录处理不存在");
            }
        } catch (Exception e) {
            log.error("删除异常记录处理失败: ID={}", processingId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "删除异常记录处理失败");
        }
    }

    @Override
    public JsonResult updateAbnormalProcessing(AbnormalProcessing abnormalProcessing) {
        log.debug("更新异常记录处理信息: {}", abnormalProcessing);

        // 参数校验
        if (abnormalProcessing == null || abnormalProcessing.getProcessingId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "处理ID不能为空");
        }

        try {
            // 检查异常记录处理是否存在
            AbnormalProcessing existingProcessing = abnormalProcessingMapper.selectById(abnormalProcessing.getProcessingId());
            if (existingProcessing == null) {
                log.warn("更新异常记录处理失败，异常记录处理不存在: ID={}", abnormalProcessing.getProcessingId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "异常记录处理不存在");
            }

            // 如果状态更新为已处理，设置处理时间
            if (abnormalProcessing.getStatus() != null &&
                abnormalProcessing.getStatus() == AbnormalProcessing.ProcessingStatus.processed &&
                existingProcessing.getStatus() != AbnormalProcessing.ProcessingStatus.processed) {
                abnormalProcessing.setProcessedAt(LocalDateTime.now());
            }

            int result = abnormalProcessingMapper.updateById(abnormalProcessing);
            if (result > 0) {
                log.info("更新异常记录处理成功: ID={}", abnormalProcessing.getProcessingId());
                return JsonResult.ok();
            } else {
                log.warn("更新异常记录处理失败: ID={}", abnormalProcessing.getProcessingId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "更新异常记录处理失败");
            }
        } catch (Exception e) {
            log.error("更新异常记录处理失败: ID={}", abnormalProcessing.getProcessingId(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "更新异常记录处理失败");
        }
    }
}
